package net.ojbk.study.curriculumDesign.beans.cilentservicer;

import net.ojbk.study.curriculumDesign.beans.canvasmodel.Message;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.common.cilentservicer.ChatStatus;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/7
 * Time: 15:50
 * Description: No Description
 */
public class Chat implements ChatStatus {
    private User cuser;
    private Servicer servicer;
    private List<ChatMessage> msgList;
    private List<ChatMessage> cuserNewMsgList;
    private List<ChatMessage> suserNewMsgList;
    /**
     * 1 请求中， 2 连接好客服，通话中 3已断开未评论 4已完成评论
     */
    private int status;
    private Date beginTime;

    public static Chat createNewChat(User cuser) {
        return new Chat(cuser, ChatStatus.STATUS_REQUESTING, new Date());
    }

    /* 功能区 */

    /**
     * Chat成面上的 结束会话
     *
     * @param score
     */
    public void endChat(double score) throws Exception {
        if (this.status != this.STATUS_CATED)
            throw new Exception("此会话不为cuted状态,请检查。");
        if (score > 100 || score < 0)
            throw new Exception("分数不符合要求");
        this.status = this.STATUS_END;
        // 设置客服的评分即可
        double divScore = this.servicer.getDivScore();
        double newDivScore = (this.servicer.getServicedSum() * divScore + score) / (this.servicer.getServicedSum() + 1);
        this.servicer.setDivScore(newDivScore);
        this.servicer.setServicedSum(this.servicer.getServicedSum() + 1);
    }

    public void joinServicer(Servicer servicer) {
        this.servicer = servicer;
        this.msgList = new ArrayList<ChatMessage>();
        this.cuserNewMsgList = new ArrayList<ChatMessage>();
        this.suserNewMsgList = new ArrayList<ChatMessage>();
        this.status = this.STATUS_CHATING;
    }

    /**
     * 返回此次会话的结果账单，方便存入数据库
     *
     * @param score 评分
     * @return 结果账单
     */
    public ServiceRecord getBill(double score) {
        return new ServiceRecord(this.servicer.getUid(), this.cuser.getUid(), this.beginTime, new Date(), score);
    }

    /**
     * 切断连接
     */
    public void catChat() throws Exception {
        // 设置状态为切断
        if (this.status != this.STATUS_CHATING)
            throw new Exception("此会话不为进行状态。");
        this.status = this.STATUS_CATED;
    }

    /**
     * 客服 -> 客户
     *
     * @param chatMessage
     */
    public void sendMsgToUser(ChatMessage chatMessage) throws Exception {
        if (chatMessage == null)
            throw new Exception("此消息存在问题");
        this.cuserNewMsgList.add(chatMessage);
        this.msgList.add(chatMessage);
    }

    /**
     * 客户 -> 客服
     *
     * @param chatMessage
     */
    public void sendMsgToSer(ChatMessage chatMessage) {
        this.suserNewMsgList.add(chatMessage);
        this.msgList.add(chatMessage);
    }

    /**
     * 获取客户的 未读消息
     *
     * @return
     */
    public ChatMessage getCustomerNewMsgList() throws Exception {
        if (this.cuserNewMsgList == null || this.cuserNewMsgList.isEmpty())
            throw new Exception("消息为空");
        ChatMessage chatMessage = this.cuserNewMsgList.get(0);
        this.cuserNewMsgList.remove(0);
        return chatMessage;
    }

    public ChatMessage getServicerNewMsgList() throws Exception {
        if (this.suserNewMsgList == null || this.suserNewMsgList.isEmpty())
            throw new Exception("消息为空");
        ChatMessage chatMessage = this.suserNewMsgList.get(0);
        this.suserNewMsgList.remove(0);
        return chatMessage;
    }

    private Chat(User cuser, int status, Date beginTime) {
        this.cuser = cuser;
        this.status = status;
        this.beginTime = beginTime;
    }

    public Chat() {
    }

    public User getCuser() {
        return cuser;
    }

    public void setCuser(User cuser) {
        this.cuser = cuser;
    }

    public Servicer getServicer() {
        return servicer;
    }

    public void setServicer(Servicer servicer) {
        this.servicer = servicer;
    }

    public List<ChatMessage> getMsgList() {
        return msgList;
    }

    public void setMsgList(List<ChatMessage> msgList) {
        this.msgList = msgList;
    }

    public List<ChatMessage> getCuserNewMsgList() {
        return cuserNewMsgList;
    }

    public void setCuserNewMsgList(List<ChatMessage> cuserNewMsgList) {
        this.cuserNewMsgList = cuserNewMsgList;
    }

    public List<ChatMessage> getSuserNewMsgList() {
        return suserNewMsgList;
    }

    public void setSuserNewMsgList(List<ChatMessage> suserNewMsgList) {
        this.suserNewMsgList = suserNewMsgList;
    }

    public Date getBeginTime() {
        return beginTime;
    }

    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Chat{" +
                "cuser=" + cuser +
                ", status=" + status +
                ", beginTime=" + beginTime +
                '}';
    }

}
